﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Library.UI.Grid;
using QuanLyDiem.models;
using QuanLyDiem.Controllers;
namespace QuanLyDiem.DanhMuc
{
    public partial class frmKhoa : DevExpress.XtraEditors.XtraForm
    {
        public frmKhoa()
        {
            InitializeComponent();
        }

        private void frmKhoa_Load(object sender, EventArgs e)
        {
            Context db = new Context();
            gridView1.Config(true, false, false, false, false, false, false, false, false);
            gridView1.Config2(DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, true);
            khoaBindingSource.DataSource = db.Khoas;
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var khoa = gridView1.GetFocusedRow() as Khoa;
            if (khoa == null) return;
            txtmakhoa.Text = khoa.Ma_khoa;
            txttenkhoa.Text = khoa.Ten_khoa;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            btnsua.Enabled = btnxoa.Enabled = false;
            txtmakhoa.Text = txttenkhoa.Text = "";
            txtmakhoa.Focus();
        }

        private void btnthuchien_Click(object sender, EventArgs e)
        {
            try
            {
                Context db = new Context();
                var khoa = new Khoa() 
                {
                    Ma_khoa=txtmakhoa.Text,
                    Ten_khoa=txttenkhoa.Text
                };
                db.Khoas.InsertOnSubmit(khoa);
                db.SubmitChanges();
                khoaBindingSource.DataSource = db.Khoas;
                gridView1.BestFitAllColumns();
                btnsua.Enabled = btnxoa.Enabled = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            try
            {
                Context db = new Context();
                var khoa = gridView1.GetFocusedRow() as Khoa;
                khoa.Ma_khoa = txtmakhoa.Text;
                khoa.Ten_khoa = txttenkhoa.Text;
                var update = db.Khoas.SingleOrDefault(a => a.ID_khoa == khoa.ID_khoa);
                update.Ma_khoa = khoa.Ma_khoa;
                update.Ten_khoa = khoa.Ten_khoa;
                db.SubmitChanges();
                khoaBindingSource.DataSource = db.Khoas;
                gridView1.BestFitAllColumns();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            try
            {
                Context db = new Context();
                var khoa = gridView1.GetFocusedRow() as Khoa;
                var delete = db.Khoas.SingleOrDefault(a => a.ID_khoa == khoa.ID_khoa);
                db.Khoas.DeleteOnSubmit(delete);
                db.SubmitChanges();
                khoaBindingSource.DataSource = db.Khoas;
                gridView1.BestFitAllColumns();
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi");
            }

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}